package com.wing.sell.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.exception.BusinessException;
import com.wing.product.service.DynamicService;
import com.wing.sell.dao.AgentDueDao;
import com.wing.sell.model.entity.Agent;
import com.wing.sell.model.entity.AgentDue;
import com.wing.sell.model.request.AgentDueForm;
import com.wing.sell.model.response.AgentDueVO;
import com.wing.sell.service.AgentDueService;
import com.wing.sell.service.AgentService;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 代理经销商尽调记录表-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class AgentDueServiceImpl extends ServiceImpl<AgentDueDao, AgentDue> implements AgentDueService {

    @Autowired
    private DynamicService dynamicService;

    @Autowired
    private AgentService agentService;

    @Override
    @Transactional
    public void add(AgentDueForm agentDueForm) throws  Exception{

        //校验

        AgentDue agentDue =new AgentDue();
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(AgentDue.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(agentDueForm, agentDue, copyOptions);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        String code=dynamicService.getDayNextCode("sel_agent_due");
        String day=simpleDateFormat.format(new Date());
        agentDue.setCode("D"+day+code);
        agentDue.setDay(Integer.valueOf(day));
        agentDue.setNo(Integer.valueOf(code));

        agentDue.setStatus(AgentDue.Status.oncheck);
        baseMapper.insert(agentDue);
    }




    @Override
    public List<AgentDueVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<AgentDueVO> pageList4app(Page page, Map<String, Object> params) {
        List<AgentDueVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public AgentDueVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    @Transactional
    public void update(AgentDueForm agentDueForm) throws Exception {
        AgentDue agentDue = this.getById(agentDueForm.getId());
        if (agentDue==null){
            throw new BusinessException("修改失败,未找到尽调记录！");
        }
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(AgentDue.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(agentDueForm, agentDue, copyOptions);
        this.updateById(agentDue);
    }

    @Override
    public void check(AgentDueForm agentDueForm) throws Exception {
        AgentDue agentDue = this.getById(agentDueForm.getId());
        if(agentDue == null){
            throw new BusinessException("审核失败！未查询到尽调记录");
        }
        agentDue.setStatus(agentDueForm.getStatus());
        agentDue.setAuditor(JwtUtils.getLoginUsername());
        agentDue.setAuditTime(LocalDateTime.now());
        this.updateById(agentDue);
        //查询改代理商下是否存在未审核的尽调记录，不存在修改代理商状态为待审核
        Agent agent = agentService.getBaseMapper().selectById(agentDue.getSelAgtId());
        //只是是带尽调的记录 才需求修改状态
        if(Agent.Status.wait_due.getValue() == agent.getStatus().getValue()) {
            QueryWrapper<AgentDue> agentDueQueryWrapper = new QueryWrapper<AgentDue>();
            agentDueQueryWrapper.eq("sel_agt_id", agentDue.getSelAgtId());
            agentDueQueryWrapper.eq("status", AgentDue.Status.oncheck.getValue());
            int count = this.getBaseMapper().selectCount(agentDueQueryWrapper);
            if (count == 0) {

                agent.setStatus(Agent.Status.wait_check);
                agentService.updateById(agent);
            }
        }
    }

}
